//
//  JLCFRootViewController.m
//  QTZ
//
//  Created by ski on 2017/8/1.
//  Copyright © 2017年 ski. All rights reserved.
//

#import "JLCFRootViewController.h"



@interface JLCFRootViewController ()

@end

@implementation JLCFRootViewController

- (void)openPopGestureRecognizer {
    [self.navigationController.interactivePopGestureRecognizer setEnabled:YES];
}

- (void)stopPopGestureRecognizer {
    [self.navigationController.interactivePopGestureRecognizer setEnabled:NO];
}

- (void)showCustomViewController:(JLCFRootViewController *)vc {
    self.definesPresentationContext = YES;
    vc.view.backgroundColor = [UIColor colorWithWhite:0 alpha:.5];
    vc.modalTransitionStyle = UIModalTransitionStyleCrossDissolve;
    vc.modalPresentationStyle = UIModalPresentationOverCurrentContext;
    [[JinLiCaiFuApplicationEntrace shareEntrance].currentNavController presentViewController:vc animated:YES completion:nil];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.view.backgroundColor = QTZ_White;
    self.view.frame = CGRectMake(0, 0, FULL_WIDTH, FULL_HEIGHT);
    if (!CGRectIsEmpty(_defaultFrame)) {
        self.view.frame = _defaultFrame;
    }
    self.automaticallyAdjustsScrollViewInsets = NO;
    self.edgesForExtendedLayout = UIRectEdgeNone;
}

#pragma mark - navigation bar
- (void)setTitle:(NSString *)title {
    [super setTitle:title];
    if (_navigationBar) {
        [_navigationBar setTitle:title];
    }
}

- (void)addNavigationBar {
    if (!_navigationBar) {
        
        _navigationBar = [[JLCFNavigationBar alloc] initWithFrame:CGRectMake(0, 0, self.view.width, NAVBAR_HEIGHT) needBlurEffect:YES];
        _navigationBar.title = self.title;
    }
    
    if (self.navigationController && self.navigationController.viewControllers.count > 1)
    {
        UIButton *backButton = [UIButton blackBackBtnWithTarget:self action:@selector(clickback) forControlEvents:UIControlEventTouchUpInside];
        [_navigationBar setLeftBarButton:backButton];
    }
    
    //设置shadow色值
    [_navigationBar setBottomBorderColor:QTZ_White];
    
    if (!_navigationBar.superview) {
        [self.view addSubview:_navigationBar];
    }
    
}

#pragma mark - Back
- (void)clickback {
    if (self.navigationController) {
        [self.navigationController popViewControllerAnimated:YES];
    }
}

#pragma mark -  网络不好的提示
- (void)showErrorTipsOnOwnerView:(UIView *)ownerView {
    
    if (!_JLCFErrorTipsView) {
        _JLCFErrorTipsView = [[JLCFErrorTipsView alloc] initWithFrame:ownerView.bounds];
        _JLCFErrorTipsView.delegate = self;
        [ownerView addSubview:_JLCFErrorTipsView];
    }
    else
    {
        [_JLCFErrorTipsView didFinishLoading];
    }
    
}

- (void)showErrorTips {
    
    if (!_JLCFErrorTipsView) {
        if (_navigationBar) {
            _JLCFErrorTipsView = [[JLCFErrorTipsView alloc] initWithFrame:CGRectMake(0, NAVBAR_HEIGHT, self.view.width, self.view.height - NAVBAR_HEIGHT)];
            [self.view insertSubview:_JLCFErrorTipsView belowSubview:_navigationBar];
        } else {
            _JLCFErrorTipsView = [[JLCFErrorTipsView alloc] initWithFrame:self.view.bounds];
            [self.view addSubview:_JLCFErrorTipsView];
        }
        _JLCFErrorTipsView.delegate = self;
        
    }
    else {
        [_JLCFErrorTipsView didFinishLoading];
    }
    
}

- (void)hideErrorTips {
    [_JLCFErrorTipsView removeFromSuperview];
    _JLCFErrorTipsView = nil;
}

#pragma mark - Notice view

- (void)showLoadingView:(UIImage *)image {
    
    [self hideNotice];
    
    _noticeView = [[MBProgressHUD alloc] initWithView:[JinLiCaiFuApplicationEntrace shareEntrance].window];
    [self.view addSubview:_noticeView];
    _noticeView.userInteractionEnabled = NO;
    _noticeView.bezelView.color = QTZ_Black;
//    // Make the customViews 37 by 37 pixels for best results (those are the bounds of the build-in progress indicators)
    _noticeView.customView = [[UIImageView alloc] initWithImage:image];
    
    // Set custom view mode
    _noticeView.mode = MBProgressHUDModeIndeterminate;
    _noticeView.centerY = self.view.centerY;
    
    [_noticeView showAnimated:YES];
}

- (void)showNotice:(NSString *)notice image:(UIImage *)image afterDelay:(CGFloat)delay {
    [self hideNotice];
    
    _noticeView = [[MBProgressHUD alloc] initWithView:[JinLiCaiFuApplicationEntrace shareEntrance].window];
    [self.view addSubview:_noticeView];
    _noticeView.userInteractionEnabled = NO;
//    _noticeView.bezelView.style = MBProgressHUDBackgroundStyleSolidColor;
//    _noticeView.bezelView.color = [UIColor clearColor];
//    // Make the customViews 37 by 37 pixels for best results (those are the bounds of the build-in progress indicators)
    _noticeView.customView = [[UIImageView alloc] initWithImage:image];
    
    _noticeView.bezelView.color = [UIColor clearColor];
    _noticeView.alpha = .8;
    
    // Set custom view mode
    _noticeView.mode = MBProgressHUDModeCustomView;
    _noticeView.removeFromSuperViewOnHide = YES;
    
    _noticeView.centerY = self.view.centerY;
    _noticeView.detailsLabel.text = notice;
    [UIActivityIndicatorView appearanceWhenContainedInInstancesOfClasses:@[[MBProgressHUD class]]].color = QTZ_White;
    _noticeView.detailsLabel.font = FONT(14);
    _noticeView.detailsLabel.textColor = QTZ_White;
    _noticeView.margin = 12.f;
    
    [_noticeView showAnimated:YES];
    [_noticeView hideAnimated:YES afterDelay:delay];
}

- (void)showNotice:(NSString *)notice {
    [self showNoticeOnWindow:notice duration:1.5];
}

- (void)showNoticeOnWindow:(NSString *)notice duration:(NSTimeInterval)duration {
//    [self hideDefaultView];
    if (!IsEmptyString(notice)) {
        
        [self hideNotice];
        
        _noticeView = [[MBProgressHUD alloc] initWithView:[JinLiCaiFuApplicationEntrace shareEntrance].window];
        [[JinLiCaiFuApplicationEntrace shareEntrance].window addSubview:_noticeView];
        _noticeView.userInteractionEnabled = YES;
        //    _noticeView.customView = [[UIImageView alloc] initWithImage:nil];
        _noticeView.mode = MBProgressHUDModeText;
        //    [UIActivityIndicatorView appearanceWhenContainedIn:[MBProgressHUD class], nil].color = QTZ_White;
        [UIActivityIndicatorView appearanceWhenContainedInInstancesOfClasses:@[[MBProgressHUD class]]].color = QTZ_White;
        _noticeView.centerY = self.view.centerY;
        _noticeView.detailsLabel.text = notice;
        
        _noticeView.removeFromSuperViewOnHide = YES;
        _noticeView.detailsLabel.font = FONT(14);
        _noticeView.detailsLabel.textColor = QTZ_White;
        _noticeView.margin = 20;
        _noticeView.bezelView.color = QTZ_Black;
        
        [_noticeView showAnimated:YES];
        [_noticeView hideAnimated:YES afterDelay:duration];
    }
}

- (void)hideNotice {
    if (nil != _noticeView) {
        [_noticeView removeFromSuperview];
        [_noticeView hideAnimated:YES afterDelay:0.f];
    }
}

@end
